import Vue from "vue";
import Router from "vue-router";
import Home from "@/views/Home.vue";
import userUtils from "./utils/userUtil";
import { unix } from "dayjs";

// 解决点击logo时，tab原先就处在studentmanage路由下，造成重新导航报错
// const originalPush = Router.prototype.push
// Router.prototype.push = function push (location) {
//   return originalPush.call(this, location).catch(err => err)
// }

Vue.use(Router);

const router = new Router({
  routes: [
    // 首页路由重定向到学生管理
    {
      path: "/",
      name: "home",
      component: Home,
      // redirect: "/StudentManage",
      children: [
        // 学生管理路由
        {
          path: "/StudentManage",
          meta: {
            title: "学生管理",
            needLogin: true
          },
          name: "StudentManage",
          // route level code-splitting
          // this generates a separate chunk (about.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () =>
            import(/* webpackChunkName: "home" */ "./views/StudentManage")
        },
        // 布置作业路由
        {
          path: "StudentManage/pubWork",
          meta: {
            title: "布置作业",
            needLogin: true
          },
          name: "pubWork",
          // route level code-splitting
          // this generates a separate chunk (about.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () =>
            import(
              /* webpackChunkName: "home" */ "./views/StudentManage/PubWork"
            )
        },
        // 练习管理路由
        {
          path: "/Exercises",
          meta: {
            title: "习题管理",
            needLogin: true
          },
          name: "Exercises",
          // route level code-splitting
          // this generates a separate chunk (about.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () =>
            import(/* webpackChunkName: "home" */ "./views/Exercises")
        },
        {
          path: "Exercises/pubWork",
          meta: {
            title: "练习布置作业",
            needLogin: true
          },
          name: "ExercisepubWork",
          component: () =>
            import(/* webpackChunkName: "home" */ "./views/Exercises/PubWork")
        },
        // 今日作业路由
        {
          path: "/TodayWork",
          meta: {
            title: "今日作业",
            needLogin: true
          },
          name: "TodayWork",
          // route level code-splitting
          // this generates a separate chunk (about.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () =>
            import(/* webpackChunkName: "home" */ "./views/TodayWork")
        },
        // 今日作业列表
        {
          path: "TodayWork/TodayWorkList",
          meta: {
            title: "作业列表",
            needLogin: true
          },
          name: "TodayWorkList",
          component: () =>
            import(
              /* webpackChunkName: "home" */ "./views/TodayWork/TodayWorkList"
            )
        },
        // 检查作业路由
        {
          path: "/CheckWork",
          meta: {
            title: "检查作业",
            needLogin: true
          },
          name: "CheckWork",
          // route level code-splitting
          // this generates a separate chunk (about.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () =>
            import(/* webpackChunkName: "home" */ "./views/CheckWork/Check")
        },
        // 登录页路由
        {
          path: "/Login",
          meta: {
            title: "登录",
          },
          name: "login",
          component: () =>
            import(/* webpackChunkName: "home" */ "./views/Login")
        },
        // 查看作业列表路由
        {
          path: "/CheckWork/workList",
          meta: {
            title: "查看作业",
          },
          name: "workList",
          component: () =>
            import(/* webpackChunkName: "home" */ "./views/CheckWork/WorkList")
        },
        // 作业详情路由
        {
          path: "/CheckWork/ExerciseDetail",
          meta: {
            title: "作业详情",
          },
          name: "ExerciseDetail",
          component: () =>
            import(
              /* webpackChunkName: "home" */ "./views/CheckWork/ExerciseDetail"
            )
        }
      ]
    }
  ]
});

// 添加路由守卫、导航守卫
router.beforeEach((to, from, next) => {
  let { title, needLogin } = to.meta;
  let isLogin = userUtils.isLogin();
  document.title = title;
  if (needLogin && !isLogin) {
    next({
      path: "Login",
      // query参数是为了设置登陆后路由重定向到之前访问的路由的
      query: {
        redirect: to.fullPath,
        hostname: location.hostname
      }
    });
    sessionStorage.setItem('redirect',to.fullPath)
    sessionStorage.setItem('hostname',location.hostname)
  } else {
    if(location.hostname=='Login'){
      console.log('login');
    }
    next();
  }
});

export default router;
